package exp.parser;

import error.processing.MyErrorProcessing;
import grammar.parser.FuncFParams;
import word.parser.WordElement;

public class PrimaryExp extends UnaryExp {
    private final ExpParser exp;

    public PrimaryExp(ExpParser exp) {
        super(exp.isComputable(), exp.isConst());
        this.exp = exp;
    }

    public static PrimaryExp parsePrimaryExp() {
        ExpParser exp = null;
        if (getAnalyzingWord().getType() == WordElement.WordType.LPARENT) {
            analyzeNextWord();
            exp = Exp.buildExp();


            if (getAnalyzingWord().getType() != WordElement.WordType.RPARENT) {
                MyErrorProcessing.buildErrorProcessing(MyErrorProcessing.ErrorType.j,
                        getAnalyzingWord(-1));
            } else {
                analyzeNextWord();
            }


        } else if (getAnalyzingWord().getType() == WordElement.WordType.INTCON) {
            exp = IntConstExp.buildIntConstExp();
        } else {
            exp = LVal.buildLVal();
        }

        return new PrimaryExp(exp);
    }

    @Override
    public LVal toLVal() {
        return exp.toLVal();
    }

    @Override
    public FuncFParams.FuncFParam checkDimension() {
        return exp.checkDimension();
    }

    @Override
    public Integer compute() {
        return exp.compute();
    }
}
